using System;
using System.Collections.Generic;
using System.Text;

namespace Lu62Viewer
{
    public class MyCompareResult
    {
        public enum eResult { Ok, DifferentFieldsCount, DifferentSegmentName, DifferentField };
        private bool m_fieldsCountCheck;
        private eResult m_result = eResult.Ok;
        private string m_segmentAName = string.Empty;
        private int m_countFieldsA;

        public int CountFieldsA
        {
            get { return m_countFieldsA; }
            set { m_countFieldsA = value; }
        }
        private int m_countFieldsB;

        public int CountFieldsB
        {
            get { return m_countFieldsB; }
            set { m_countFieldsB = value; }
        }

        public MyCompareResult(MySegmentNode _segmentA, MySegmentNode _segmentB)
        {
            if (_segmentA == null)
                throw new ArgumentNullException("_segmentA");
            if (_segmentB == null)
                throw new ArgumentNullException("_segmentB");            
            compareSegment(_segmentA, _segmentB);
        }

        public string SegmentAName
        {
            get { return m_segmentAName; }
            set { 
                m_segmentAName = value;
                if (m_segmentAName.CompareTo(m_segmentBName) != 0)
                    m_result = eResult.DifferentSegmentName;
                else 
                    m_result = eResult.Ok;
            }
        }
        private string m_segmentBName = string.Empty;

        public string SegmentBName
        {
            get { return m_segmentBName; }
            set { 
                m_segmentBName = value;
                if (m_segmentBName.CompareTo(m_segmentAName) != 0)
                    m_result = eResult.DifferentSegmentName;
                else
                    m_result = eResult.Ok;
            }
        }

        public eResult Result
        {
            get { return m_result; }            
        }

        public bool FieldsCountCheck
        {
            get { return m_fieldsCountCheck; }
            set { 
                m_fieldsCountCheck = value;
                if (value == false)
                {
                    m_result = eResult.DifferentFieldsCount;
                }                
            }
        }        

        private List<MyFieldError> m_fieldErrors;

        public List<MyFieldError> FieldErrors
        {
            get {
                if (m_fieldErrors == null)
                    m_fieldErrors = new List<MyFieldError>();
                return m_fieldErrors; 
            }
            set { 
                m_fieldErrors = value;
                if (m_result == eResult.Ok)
                    m_result = eResult.DifferentField;
            }
        }

        private const string campoNonTrovato = "campo non trovato";

        private void compareSegment(MySegmentNode _segmentA, MySegmentNode _segmentB)
        {  
            SegmentAName = _segmentA.Text;
            SegmentBName = _segmentB.Text;
            
            m_countFieldsA = _segmentA.Nodes.Count;
            m_countFieldsB = _segmentB.Nodes.Count;
            if (CountFieldsA != CountFieldsB)
            {
                m_result = eResult.DifferentFieldsCount;
                FieldsCountCheck = false;
            }
            else
            {
                FieldsCountCheck = true;
            }
                
            for (int i = 0; i <= _segmentA.Nodes.Count - 1; i++)
            {
                string a = campoNonTrovato;
                string b = campoNonTrovato;
                try
                {
                    a = _segmentA.Nodes[i].Text;
                }
                catch { }
                if (i <= CountFieldsB)
                {
                    try
                    {
                        b = _segmentB.Nodes[i].Text;
                    }
                    catch { }
                }
                if (a == campoNonTrovato || b == campoNonTrovato || a.CompareTo(b) != 0)
                {
                    // Uno dei due campi e' inesistente o non combacia con l'altro
                    MyFieldError err = new MyFieldError();
                    err.FieldA = a;
                    err.FieldB = b;
                    err.Progressivo = i + 1;
                    err.SegmentAName = SegmentAName;
                    err.SegmentBName = SegmentBName;
                    FieldErrors.Add(err);
                }
            }           
        }
    }
}
